package com.koron.housing.apply.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.OrderItem;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.housing.admin.service.HousingAdminService;
import com.koron.housing.apply.bean.dto.HousingStaffIntoDTO;
import com.koron.housing.apply.bean.dto.HousingStaffOutDTO;
import com.koron.housing.apply.bean.po.HousingStaffApply;
import com.koron.housing.apply.bean.po.HousingStaffOut;
import com.koron.housing.apply.bean.query.HousingApplyQuery;
import com.koron.housing.apply.bean.vo.HousingStaffOutVO;
import com.koron.housing.apply.mapper.HousingStaffOutMapper;
import com.koron.housing.apply.service.HousingStaffApplyService;
import com.koron.housing.apply.service.HousingStaffIntoService;
import com.koron.housing.apply.service.HousingStaffOutService;
import com.koron.housing.staff.service.HousingStaffService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 退租申请(HousingStaffOut)表服务实现类
 *
 * @author jzp
 * @since 2022-02-26 14:55:12
 */
@Service("outService")
public class HousingStaffOutServiceImpl implements HousingStaffOutService {

    @Autowired
    private HousingStaffApplyService applyService;
    @Autowired
    private HousingStaffIntoService intoService;
    @Autowired
    private HousingAdminService adminService;
    @Autowired
    private HousingStaffService staffService;

    private String getCurrAccount() {
        return ThreadLocalContext.get().getAccount();
    }

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public HousingStaffOut getById(SessionFactory factory, String id) {
        HousingStaffOutMapper mapper = factory.getMapper(HousingStaffOutMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getVoById")
    public HousingStaffOutVO getVoById(SessionFactory factory, String id) {
        HousingStaffOutMapper mapper = factory.getMapper(HousingStaffOutMapper.class);
        return mapper.selectVoById(id);
    }

    @Override
    @TaskAnnotation("getDtoById")
    public HousingStaffOutDTO getDtoById(SessionFactory factory, String id) {
        HousingStaffOutVO housingStaffOutVO = getVoById(factory, id);
        HousingStaffOutDTO outDTO = BeanUtil.toBean(housingStaffOutVO, HousingStaffOutDTO.class);
        HousingStaffIntoDTO intoDTO = intoService.getDtoById(factory, outDTO.getIntoId());
        outDTO.setIntoDTO(intoDTO);
        return outDTO;
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<HousingStaffOutVO> list(SessionFactory factory, HousingApplyQuery query) {
        HousingStaffOutMapper mapper = factory.getMapper(HousingStaffOutMapper.class);
        return mapper.selectVoList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<HousingStaffOutVO> page(SessionFactory factory, PageQuery pageQuery, HousingApplyQuery query) {
        /*
         * handler -> handler + account
         * account -> account
         * null -> admin or (handler + account)
         */
        if (!StringUtils.isEmpty(query.getHandler())) {
            query.setAccount(query.getHandler());
        }
        if (StringUtils.isEmpty(query.getAccount())) {
            boolean isAdmin = adminService.isAdmin(factory, getCurrAccount());
            query.setAccount(isAdmin ? null : getCurrAccount());
            query.setHandler(isAdmin ? null : getCurrAccount());
        }
        // 默认按申请单（状态（低到高）、创建时间（低到高））排序
        if (CollectionUtils.isEmpty(pageQuery.getOrders())) {
            pageQuery.getOrders().add(new OrderItem("status", true));
            pageQuery.getOrders().add(new OrderItem("createTime", true));
        }
        pageQuery.unPrefixOrders("housingStaffOut", Arrays.asList("currNodeName", "currNodeUser"));
        pageQuery.prefixOrders("housingStaffInto", Arrays.asList("address", "roomNum", "intoDate"));
        pageQuery.prefixOrders("housingStaffApply", Arrays.asList("account", "name", "deptId", "deptName"));
        return HousingStaffOutService.super.page(factory, pageQuery, query);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, HousingStaffOut housingStaffOut) {
        HousingStaffOutMapper mapper = factory.getMapper(HousingStaffOutMapper.class);

        if (StringUtils.isEmpty(housingStaffOut.getStatus())) {
            housingStaffOut.setStatus("0");
        }

        if (StringUtils.isEmpty(housingStaffOut.getCode())) {
            HousingStaffApply apply = applyService.getById(factory, housingStaffOut.getApplyId());
            housingStaffOut.setCode(apply.getCode() + "E");
        }

        return mapper.insertOrUpdate(housingStaffOut) > 0;
    }

    @Override
    @TaskAnnotation("saveOut")
    public boolean saveOut(SessionFactory factory, HousingStaffOut housingStaffOut) {
        return save(factory, housingStaffOut);
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        updateStatus(factory, id, "2");

        HousingStaffOutDTO outDTO = getDtoById(factory, id);

        // 将对应的入住信息和需求申请信息进行归档
        intoService.updateStatus(factory, outDTO.getIntoId(), "4");
        return applyService.updateStatus(factory, outDTO.getApplyId(), "3") &&
                staffService.out(factory, getDtoById(factory, id));
    }

    @Override
    @TaskAnnotation("submit")
    public boolean submit(SessionFactory factory, HousingStaffOut housingStaffOut) {
        // 保存数据
        housingStaffOut.setStatus("1");
        // 保存或更新退租信息
        return save(factory, housingStaffOut);
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, HousingStaffOut housingStaffOut) {
        HousingStaffOutMapper mapper = factory.getMapper(HousingStaffOutMapper.class);
        return mapper.update(housingStaffOut) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        HousingStaffOut housingStaffOut = new HousingStaffOut();
        housingStaffOut.setId(id);
        housingStaffOut.setStatus(status);
        return update(factory, housingStaffOut);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        HousingStaffOutMapper mapper = factory.getMapper(HousingStaffOutMapper.class);
        return mapper.hidden(id) > 0;
    }
}
